package com.hzw.saas.api.storage.enums;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import cn.hutool.core.lang.Assert;

/**
 * 存储空间
 *
 * @author zzl
 * @since 02/26/2021
 */
public enum StorageSpaceEnum {

    WEB_SPACE(SpaceCode.ROOT_CODE, "S0001", "WEB空间", true, true),
    CLOUD_DESKTOP_SPACE(SpaceCode.ROOT_CODE, "S0002", "云桌面空间", true, true),
    HBOX_SPACE(SpaceCode.ROOT_CODE, "S0003", "HBox空间", true, true),
    DEVICE_STUDIO_SPACE(SpaceCode.ROOT_CODE, "S0004", "DeviceStudio空间", true, true),

    LITERATURE_SPACE(SpaceCode.ROOT_CODE, "S0101", "文献存储空间", false, true),
    ATTACHMENT_SPACE(SpaceCode.ROOT_CODE, "S0102", "附件存储空间", false, false),
    ;
    public static final String API_TEXT = "S0001-WEB空间，S0002-云桌面空间，S0003-HBox空间，S0004-DeviceStudio空间";

    /**
     * 上级空间code
     */
    private final String supCode;

    /**
     * 空间编号
     */
    private final String code;

    /**
     * 空间名称
     */
    private final String text;

    /**
     * 空间是否对外展示
     */
    private final Boolean show;

    /**
     * 是否限制空间大小
     */
    private final Boolean limit;

    StorageSpaceEnum(String supCode, String code, String text, Boolean show, Boolean limit) {
        this.supCode = supCode;
        this.code = code;
        this.text = text;
        this.show = show;
        this.limit = limit;
    }

    public String getCode() {
        return code;
    }

    public String getText() {
        return text;
    }

    public Boolean getShow() {
        return show;
    }

    public Boolean getLimit() {
        return limit;
    }

    public static String rootCode() {
        return SpaceCode.ROOT_CODE;
    }

    public static String getText(String code) {
        for (StorageSpaceEnum value : StorageSpaceEnum.values()) {
            if (Objects.equals(value.code, code))
                return value.text;
        }
        return "";
    }

    public static boolean isLimit(String code) {
        StorageSpaceEnum instance = assertInstance(code);
        return Objects.equals(instance.limit, true);
    }

    public static boolean isValid(String code) {
        StorageSpaceEnum instance = instance(code);
        return Objects.nonNull(instance);
    }

    public static void assertValid(String code) {
        StorageSpaceEnum instance = instance(code);
        Assert.notNull(instance, "存储空间异常: {}", code);
    }

    public static StorageSpaceEnum instance(String code) {
        for (StorageSpaceEnum value : StorageSpaceEnum.values()) {
            if (Objects.equals(value.code, code))
                return value;
        }
        return null;
    }

    public static List<String> listCodes(String supCode, Boolean show, Boolean limit) {
        List<String> result = new ArrayList<>();
        for (StorageSpaceEnum value : StorageSpaceEnum.values()) {
            if (Objects.nonNull(supCode) && !Objects.equals(value.supCode, supCode)) {
                continue;
            }
            if (Objects.nonNull(show) && !Objects.equals(value.show, show)) {
                continue;
            }
            if (Objects.nonNull(limit) && !Objects.equals(value.limit, limit)) {
                continue;
            }
            result.add(value.code);
        }
        return result;
    }

    public static StorageSpaceEnum assertInstance(String code) {
        StorageSpaceEnum instance = instance(code);
        Assert.notNull(instance, "存储空间异常{}", code);
        return instance;
    }

    private static class SpaceCode {
        private static final String ROOT_CODE = "00";
    }

}
